package com.ruoyi.demo.controller.app;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.demo.domain.AuctionOrder;
import com.ruoyi.demo.domain.vo.AuctionOrderVO;
import com.ruoyi.demo.service.IAuctionOrderService;
import com.ruoyi.demo.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "小程序-订单")
@RestController
@RequestMapping("/app/order")
public class AppAuctionOrderController extends BaseController {
    @Autowired
    private IAuctionOrderService auctionOrderService;

    @ApiOperation("查询订单列表")
    @GetMapping("/list")
    public TableDataInfo list(AuctionOrder auctionOrder) {
        startPage();
        List<AuctionOrderVO> list = auctionOrderService.selectAuctionOrderVOList(auctionOrder);
        return getDataTable(list);
    }

    @ApiOperation("获取我的订单列表")
    @GetMapping("/my")
    public TableDataInfo myOrders(@RequestHeader("token") String token) {
        Long userId = TokenUtils.getUserIdFromToken(token);
        if (userId == null) {
            return null;
        }
        startPage();
        AuctionOrder queryParam = new AuctionOrder();
        queryParam.setBuyerId(userId);
        List<AuctionOrderVO> list = auctionOrderService.selectAuctionOrderVOList(queryParam);
        return getDataTable(list);
    }

    @ApiOperation("获取订单详细信息")
    @GetMapping("/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId) {
        AuctionOrderVO orderVO = auctionOrderService.selectAuctionOrderVOByOrderId(orderId);
        return success(orderVO);
    }

    @ApiOperation("新增订单")
    @PostMapping
    public AjaxResult add(@RequestBody AuctionOrder auctionOrder) {
        return toAjax(auctionOrderService.insertAuctionOrder(auctionOrder));
    }

    @ApiOperation("修改订单")
    @PutMapping
    public AjaxResult edit(@RequestBody AuctionOrder auctionOrder) {
        return toAjax(auctionOrderService.updateAuctionOrder(auctionOrder));
    }

    @ApiOperation("删除订单")
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds) {
        return toAjax(auctionOrderService.deleteAuctionOrderByOrderIds(orderIds));
    }

    @ApiOperation("订单成交")
    @PostMapping("/deal/{goodsId}")
    public AjaxResult deal(@PathVariable("goodsId") Long goodsId) {
        return toAjax(auctionOrderService.createDealOrder(goodsId));
    }

    @ApiOperation("订单支付")
    @PostMapping("/{orderId}/pay")
    public AjaxResult pay(@PathVariable("orderId") Long orderId) {
        return toAjax(auctionOrderService.payOrder(orderId));
    }

    @ApiOperation("取消订单")
    @PostMapping("/{orderId}/cancel")
    public AjaxResult cancel(@PathVariable("orderId") Long orderId) {
        return toAjax(auctionOrderService.cancelOrder(orderId));
    }
}
